package ktvutil

import (
	"encoding/binary"
	"time"
)

// 版权信息
type DatCopyright struct {
	Copyright   [8]byte
	Version     uint32
	FileSize    uint32
	CreatedDate uint32
	RecordCount uint32
}

func (d *DatCopyright) GetStructSize() uint32 {
	//  8 + 4*4 = 24
	return 24
}

// 获取版权信息结构体大小
func GetCopyrightStructLength() uint32 {
	var dc DatCopyright
	return dc.GetStructSize()
}

// byte[] 转换为
func GetCopyrightInfo(buf []byte) *DatCopyright {

	copyright := new(DatCopyright)

	// 如果长度不够，则返回空的copyright
	if uint32(len(buf)) < GetCopyrightStructLength() {
		return copyright
	}

	for i := 0; i < 8; i++ {
		copyright.Copyright[i] = buf[i]
	}

	copyright.Version = binary.LittleEndian.Uint32(buf[8:12])
	copyright.FileSize = binary.LittleEndian.Uint32(buf[12:16])
	copyright.CreatedDate = binary.LittleEndian.Uint32(buf[16:20])
	copyright.RecordCount = binary.LittleEndian.Uint32(buf[20:24])

	return copyright
}

// 是否为SYSDKTV的标记
func (d *DatCopyright) IsShiyou() bool {
	// SYSDKTV\0
	mark := "SYSDKTV"
	m1 := []byte(mark)
	//shiyou := {0x53, 0x59, 0x53, 0x44, 0x4b, 0x54, 0x56, 0x00}

	for i := 0; i < 7; i++ {
		if m1[i] != d.Copyright[i] {
			return false
		}
	}

	return true
}

func (d *DatCopyright) Reset() {

	// SYSDKTV\0
	mark := "SYSDKTV"

	for i := 0; i < 7; i++ {
		d.Copyright[i] = byte(mark[i])
	}

	d.CreatedDate = uint32(time.Now().Unix())
	d.RecordCount = 0
	d.FileSize = d.GetStructSize()
	d.Version = 0

}
